<?php


namespace app\api\controller;


use app\common\controller\ApiController;
use app\common\model\TaskRelease;
use app\common\model\TaskUser as TaskUserModel;
use think\Exception;

class taskUser extends ApiController
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new TaskUserModel();
    }

    public function create()
    {

    }

    /**
     * 任务领取
     *
     */
    public function claim()
    {

        $task_id = input('task_id', '');
        if(empty($task_id)){ self::_result([], -1, '发布任务id不能为空'); }

        try{
            $task_release_model = new TaskRelease();
            $task_release_model->claim($task_id, $this->userId);
        }catch (Exception $e){
            self::_result([], -1, $e->getMessage());
        }
        self::_result();
    }

    /**
     * 任务完成
     *
     */
    public function completed()
    {

        $task_id = input('task_id', '');
        $finish_img = input('img' ,'');
        if(empty($task_id)){ self::_result([], -1, '发布任务id不能为空'); }
        elseif(empty($finish_img)){ self::_result([], -1, '现场图片不能为空'); }

        try{
            $data = [];
            $data['task_id'] = $task_id;
            $data['img'] = $finish_img;
            $this->model->completed($data, $this->userId);
        }catch (\Exception $e){
            self::_result([], -1, $e->getMessage());
        }
        self::_result();
    }

    /**
     * 获取列表
     *
     */
    public function list()
    {

        $page = input('page', 1);
        $page_size = input('page_size', 10);
        $status = input('status', '');

        $result = $conditions = array();
        $conditions[] = array('a.claim_uid', '=', $this->userId);
        $status === '' ?: $conditions[] = array('a.status', '=', $status);
        try{
            $result = $this->model->getListInfo($conditions, $page, $page_size);
        }catch (\Exception $e){
            self::_result([], -1, $e->getMessage());
        }
        self::_result($result);
    }

    /**
     * 获取详情
     *
     */
    public function detail()
    {

        $id = input('id', '');
        if(empty($id)){ self::_result([], -1, 'id不能为空'); }

        $result = $conditions = array();
        $conditions[] = array('a.id', '=', $id);
        $conditions[] = array('a.claim_uid', '=', $this->userId);
        try{
            $result = $this->model->getDetail($conditions);
        }catch (\Exception $e){
            self::_result([], -1, $e->getMessage());
        }
        self::_result($result);
    }
}